<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    /*
        WeakMap和WeakSet是ES6新增的数据结构，它们的作用是为了解决内存泄漏的问题。
    弱引用，防止内存泄漏
    WeakMap只能用对象作为键，值可以是任意类型。WeakSet只能用对象做value
    没有forEach和size，只能用set delete has get   没有forEach和size，

    
    */
    /*  const a = {}
     function fn() {
         const obj = { name: 'zs' }
         a.obj = obj  //强引用
     }
     fn()
     console.log(a.obj); */
    const wMap = new WeakMap()
    wMap.set({}, 'heihei')
    function fn() {
        const obj = { name: 'zs' }
        wMap.set(obj, 'zs')
    }
    fn()
    console.log(wMap);
    //WeakMap 场景      
    const userInfo = { name: 'zs' }
    const cityInfo = { city: 'beijing' }
    wMap.set(userInfo, cityInfo) //建立一种关联关系，而且两者保持独立，而且不影响彼此的销毁逻辑
    console.log(wMap.get(userInfo)) // {city: "beijing"}


    //WeakSet 
    let wSet = new WeakSet()
    function fn1() {
        const obj = { name: 'zs' }
        wSet.add(obj)
    }
    fn1()
    console.log(wSet, 'wSet');
   
</script>

</html>